package io.renren.util;

import java.util.Random;

/**
 * Created by ThinkPad on 2018/10/9.
 */
public class RandomCard {
    private static final char[] numArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    private static final char[] letterArray = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    private static final char[] numAndLetterArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    public RandomCard() {
    }

    public static String genRandomNum(int length) {
        return getRandomNum(length, RandomCard.RandomFormat.NUM_AND_LETTER);
    }

    public static String getRandomNum(int length, RandomCard.RandomFormat randomFormat) {
        char[] strArray;
        switch(randomFormat) {
            case NUM:
                strArray = numArray;
                break;
            case LETTER:
                strArray = letterArray;
                break;
            case NUM_AND_LETTER:
                strArray = numAndLetterArray;
                break;
            default:
                throw new IllegalArgumentException();
        }

        int maxNum = strArray.length;
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        int count = 0;

        while(count < length) {
            int i = Math.abs(r.nextInt(maxNum));
            if(i >= 0 && i < strArray.length) {
                pwd.append(strArray[i]);
                ++count;
            }
        }

        return pwd.toString();
    }

    public static enum RandomFormat {
        NUM,
        LETTER,
        NUM_AND_LETTER;

        private RandomFormat() {
        }
    }

}
